package org.shj.hadoop.fs;

import java.io.IOException;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;

public class HdfsUtil {
	
	private static FileSystem fs = null;
	
	private static void init() throws Exception{
		Configuration cfg = new Configuration();

		fs = FileSystem.get(new URI("hdfs://192.168.56.101:9000"),cfg, "shj");
	}

	/**
	 * 
	 * @param src ���ص��ļ�·��
	 * @param dest hadoop hdfs ���ļ�·��
	 * @throws IOException
	 */
	public static void copyFromLocal(String src, String dest) throws Exception{
		init();		
		fs.copyFromLocalFile(new Path(src), new Path(dest));
		fs.close();
	}
	
	public static void downloadFile(String src, String dest)throws Exception{
		init();
		fs.copyToLocalFile(false, new Path(src), new Path(dest), true);
		fs.close();
	}
	
	public static void makeDir()throws Exception{
		init();
		Path p = new Path("/aaa");
		fs.mkdirs(p);
		System.out.println("�Ѵ���");
		
		boolean exists = fs.exists(p);
		System.out.println(exists);
		
		fs.delete(p, true);
		System.out.println("��ɾ��");
		
		exists = fs.exists(p);
		System.out.println(exists);
		
		fs.close();
	}
	
	public static void fileStatus(String path)throws Exception{
		init();
		
		Path p = new Path(path);
		//ֻ���г��ļ����������ļ���
		RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(p, true);
		LocatedFileStatus next = null;
		while(listFiles.hasNext()){
			next = listFiles.next();
			System.out.println(next.getPath().getName());
		}
		
		System.out.println("-----------------------------");
		
		FileStatus[] listStatus = fs.listStatus(p);
		for(FileStatus fileStat : listStatus){
			if(fileStat.isDirectory()){
				System.out.print("D\t");
			}
			System.out.println(fileStat.getPath().getName());
		}
		
		
		fs.close();
	}
	
	
	public static void testOther(String path)throws Exception{
		init();
		
		Path p = new Path(path);
		FileStatus fileStatus = fs.getFileStatus(p);
		
		//����ļ�����128M�Ļ������Բ鿴�ֿ��ļ����������̨������
		BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(p, 0, fileStatus.getLen());
		for(BlockLocation bl : fileBlockLocations){
			System.out.println(bl.getOffset());//�ֿ��ļ�����ʼλ��
			System.out.println(bl.getNames()[0]);
		}
		
		//�����ļ��ĸ�������
		fs.setReplication(p, (short)3);
		
		fs.close();
	}
	
	public static void main(String[] args) throws Exception{
		//1. copyFromLocal
//		String src = "D:\\source\\android\\dex2jar-0.0.9.15\\NOTICE.txt";
//		String dest = "/"; 
//		HdfsUtil.copyFromLocal(src, dest);
		
		//2. downloadFile
//		String src = "/user/shj/test.txt";
//		String dest = "d:\\"; 
//		HdfsUtil.downloadFile(src, dest);
		
//		HdfsUtil.makeDir();
		
		HdfsUtil.fileStatus("/");
	}
}
